package com.example.li.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMConnectionListener;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.util.NetUtils;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private EditText user;
    private EditText psw;
    private MyConnectionListener myConnectionListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册一个监听连接状态的listener
        myConnectionListener = new MyConnectionListener();
        EMClient.getInstance().addConnectionListener(myConnectionListener);
        intoView();
    }

    private void intoView() {
        user = findViewById(R.id.user);
        psw = findViewById(R.id.psw);
    }


    public void zc(View view) {
        final String user = this.user.getText().toString();
        final String psw = this.psw.getText().toString();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().createAccount(user, psw);//同步方法
                    Log.d(TAG, "run: 注册成功");
                } catch (HyphenateException e) {
                    Log.d(TAG, "run: 注册失败");
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void dl(View view) {
        String user = this.user.getText().toString();
        String psw = this.psw.getText().toString();
        EMClient.getInstance().login(user, psw, new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                Log.d(TAG, "登录聊天服务器成功！");
                Intent intent = new Intent(MainActivity.this,HomeActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d(TAG, "onError: " + message);
                Log.d(TAG, "登录聊天服务器失败！");
            }
        });
    }

    //实现ConnectionListener接口
    private class MyConnectionListener implements EMConnectionListener {
        @Override
        public void onConnected() {
            //重新连接上网络
            Log.d(TAG, "onConnected: 重新连接上网络");
        }

        @Override
        public void onDisconnected(final int error) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (error == EMError.USER_REMOVED) {
                        // 显示帐号已经被移除
                        Log.d(TAG, "run: 显示帐号已经被移除");
                    } else if (error == EMError.USER_LOGIN_ANOTHER_DEVICE) {
                        // 显示帐号在其他设备登录
                        Log.d(TAG, "run: 显示帐号在其他设备登录");
                    } else {
                        if (NetUtils.hasNetwork(MainActivity.this)) {
                            //连接不到聊天服务器
                            Log.d(TAG, "run: 连接不到聊天服务器");
                        } else {
                            //当前网络不可用，请检查网络设置
                            Log.d(TAG, "run:当前网络不可用，请检查网络设置 ");
                        }
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EMClient.getInstance().removeConnectionListener(myConnectionListener);
    }
}
